package com.dys.flex;

import com.dys.entity.SysUser;
import com.dys.mapper.SysUserMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

import static com.dys.entity.table.SysUserTableDef.SYS_USER;

/**
 * @Author: 疾风
 * @ClassName: UpdateData
 * @Description: 更新数据
 * @CreateDate: 2025/01/14
 * @Version: v1.0.0
 */
@SpringBootTest
public class UpdateData {

    @Autowired
    private SysUserMapper userMapper;

    @Test
    void testUpdateById(){
        int row = 0;
        SysUser user = new SysUser();
        user.setId(20L);
        user.setNickName("开发人员");
        user.setUserName("dev");
        // 默认忽略null值
        row = userMapper.update(user);
        // 不忽略null值,设置为false
        // row = userMapper.update(user,false);
        System.out.println("受影响行数："+row);
    }

    @Test
    void testUpdateByMap(){
        int row = 0;
        SysUser user = new SysUser();
        user.setNickName("运维人员");
        user.setUserName("运维主管");
        Map<String, Object> map = new HashMap<>();
        map.put("nick_name","开发人员");
        row = userMapper.updateByMap(user,map);
        System.out.println("受影响行数："+row);
    }

    @Test
    void testUpdateByCondition(){
        int row = 0;
        SysUser user = new SysUser();
        user.setNickName("项目总监");
        user.setUserName("总监");
        row = userMapper.updateByCondition(user,SYS_USER.NICK_NAME.eq("运维人员"));
        System.out.println("受影响行数："+row);
    }

    @Test
    void testUpdateByQuery(){
        int row = 0;
        SysUser user = new SysUser();
        user.setNickName("项目总监");
        user.setUserName("CTO");
        row = userMapper.updateByQuery(user,QueryWrapper.create().where(SYS_USER.USER_NAME.eq("总监")));
        System.out.println("受影响行数："+row);
    }

    @Test
    void testFullUpdate(){
        int row = 0;
        SysUser user = new SysUser();
        user.setNickName("项目经理");
        // 全表更新
        Assertions.assertThrows(Exception.class, () -> {
            userMapper.updateByQuery(user,QueryWrapper.create());
        });
    }

    @Test
    void testUpdateEntity(){
        SysUser user = UpdateEntity.of(SysUser.class);
        user.setId(21L);
        user.setUserPhone(null);
        user.setStatus("0");
        // user_gender->null
        // user_name->null
        // set user_phone=null,status=0 需要使用这个：UpdateEntity
        // 使用更新: set status=0
        // 设置为false: set user_phone = null,status=0,user_name=null,user_email=null
        int row = userMapper.update(user);
        Assertions.assertEquals(1,row);
    }

    @Test
    void testUpdate(){
        // 复杂更新
        SysUser user = UpdateEntity.of(SysUser.class,21L);
        // 通过 UpdateWrapper 操作 account 数据
        UpdateWrapper<SysUser> updateWrapper = UpdateWrapper.of(user);
        updateWrapper.set(SYS_USER.USER_NAME,"研发主管");
        updateWrapper.set(SYS_USER.NICK_NAME,"研发人员");
        updateWrapper.set(SYS_USER.STATUS,"1");
        int row = userMapper.update(user);
        System.out.println("受影响行数："+row);
    }

    @Test
    void testUpdateChain(){
        boolean row = false;
        row = UpdateChain.of(SysUser.class)
                .set(SysUser::getUserName,"王大锤")
                .set(SysUser::getNickName,"大老板")
                .where(SysUser::getId).eq(21L)
                .update();
        System.out.println("更新状态："+true);
    }
}
